home *** CD-ROM | disk | FTP | other *** search
/ PC Player 2004 May / pc player 2004-05.iso / Demos / FarCry / Data1.cab / _C01238B9D66243A294BC7A016E3DB08F < prev    next >
Encoding:
Text File  |  2004-01-06  |  8.6 KB  |  331 lines

  1. ----------------------------------------------------------------------------
  2. --
  3. -- Description :        Delayed proxymity trigger
  4. --
  5. -- Create by Alberto :    03 March 2002
  6. --
  7. ----------------------------------------------------------------------------
  8. ProximityTrigger = {
  9.     type = "Trigger",
  10.  
  11.     Properties = {
  12.         DimX = 5,
  13.         DimY = 5,
  14.         DimZ = 5,
  15.         bEnabled=1,
  16.         EnterDelay=0,
  17.         ExitDelay=0,
  18.         bOnlyPlayer=1,
  19.         bOnlyMyPlayer=0,
  20.         bOnlyAI = 0,
  21.         bOnlySpecialAI = 0,
  22.         bKillOnTrigger=0,
  23.         bTriggerOnce=0,
  24.         ScriptCommand="",
  25.         PlaySequence="",        
  26.         aianchorAIAction = "",
  27.         TextInstruction= "",
  28.         bActivateWithUseButton=0,
  29.         bInVehicleOnly=0,        
  30.     },
  31.     
  32.     Editor={
  33.         Model="Objects/Editor/T.cgf",
  34.     },    
  35. }
  36.  
  37. function ProximityTrigger:OnPropertyChange()
  38.     self:OnReset();
  39. end
  40.  
  41. function ProximityTrigger:OnInit()
  42.     self:EnableUpdate(0);
  43.     self:SetUpdateType( eUT_Physics );
  44.     self:TrackColliders(1);
  45.  
  46.     self.Who = nil;
  47.     self.Entered = 0;
  48.     self.bLocked = 0;
  49.     self.bTriggered = 0;
  50.  
  51.     self:RegisterState("Inactive");
  52.     self:RegisterState("Empty");
  53.     self:RegisterState("Occupied");
  54.     self:OnReset();
  55. end
  56.  
  57. function ProximityTrigger:OnShutDown()
  58. end
  59.  
  60. function ProximityTrigger:OnSave(stm)
  61.     --WriteToStream(stm,self.Properties);    
  62.     stm:WriteInt(self.bTriggered);
  63. end
  64.  
  65.  
  66. function ProximityTrigger:OnLoad(stm)
  67.     --self.Properties=ReadFromStream(stm);
  68.     --self:OnReset();    
  69.     self.bTriggered=stm:ReadInt();
  70. end
  71.  
  72. function ProximityTrigger:OnReset()
  73.     self:KillTimer();
  74.     self.bTriggered = 0;
  75.  
  76.     local Min = { x=-self.Properties.DimX/2, y=-self.Properties.DimY/2, z=-self.Properties.DimZ/2 };
  77.     local Max = { x=self.Properties.DimX/2, y=self.Properties.DimY/2, z=self.Properties.DimZ/2 };
  78.     self:SetBBox( Min, Max );
  79.     --self:Log( "BBox:"..Min.x..","..Min.y..","..Min.z.."  "..Max.x..","..Max.y..","..Max.z );
  80.     self.Who = nil;
  81.     self.UpdateCounter = 0;
  82.     self.Entered = 0;
  83.     if(self.Properties.bEnabled==1)then
  84.         self:GotoState( "Empty" );
  85.     else
  86.         self:GotoState( "Inactive" );
  87.     end
  88.  
  89.  
  90. end
  91.  
  92. function ProximityTrigger:Event_Enter( sender )
  93.  
  94.     -- to make it not trigger when event sent to inactive tringger
  95.     if (self:GetState( ) == "Inactive") then return end
  96.  
  97.     if ((self.Entered ~= 0)) then
  98.         return
  99.     end
  100.     if (self.Properties.bTriggerOnce == 1 and self.bTriggered == 1) then
  101.         return
  102.     end
  103.     self.bTriggered = 1;
  104.     self.Entered = 1;
  105.     -- Trigger script command on enter.
  106.     if(self.Properties.ScriptCommand and self.Properties.ScriptCommand~="")then
  107.         --self:Log( "Executing: "..self.Properties.ScriptCommand);
  108.         dostring(self.Properties.ScriptCommand);
  109.     end
  110.     if(self.Properties.PlaySequence~="")then
  111.         Movie:PlaySequence( self.Properties.PlaySequence );
  112.     end
  113.  
  114.     BroadcastEvent( self,"Enter" );
  115.     AI:RegisterWithAI(self.id, 0);
  116.  
  117.     --self:Log( "Player "..sender:GetName().." Enter ProximityTrigger "..self:GetName() );
  118. end
  119.  
  120.  
  121. function ProximityTrigger:Event_Leave( sender )
  122.     if (self.Entered == 0) then
  123.         return
  124.     end
  125.     self.Entered = 0;
  126.     BroadcastEvent( self,"Leave" );
  127.  
  128.     --self:Log( "Player "..sender:GetName().." Leave ProximityTrigger "..self:GetName() );
  129.     
  130.     if(self.Properties.bTriggerOnce==1)then
  131.         self:GotoState("Inactive");
  132.     end
  133. end
  134.  
  135. function ProximityTrigger:Event_Enable( sender )
  136.     self:GotoState("Empty")
  137.     BroadcastEvent( self,"Enable" );
  138. end
  139.  
  140. function ProximityTrigger:Event_Disable( sender )
  141.     self:GotoState( "Inactive" );
  142.     AI:RegisterWithAI(self.id, 0);
  143.     BroadcastEvent( self,"Disable" );
  144. end
  145.  
  146. --function ProximityTrigger:Log( msg )
  147. --    System:LogToConsole( msg );
  148. --end
  149.  
  150. -- Check if source entity is valid for triggering.
  151. function ProximityTrigger:IsValidSource( entity )
  152.     if (self.Properties.bOnlyPlayer ~= 0 and entity.type ~= "Player") then
  153.         return 0;
  154.     end
  155.  
  156.     if (self.Properties.bOnlySpecialAI ~= 0 and entity.ai ~= nil and entity.Properties.special==0) then 
  157.         return 0;
  158.     end
  159.  
  160.     -- if Only for AI, then check
  161.     if (self.Properties.bOnlyAI ~=0 and entity.ai == nil) then
  162.         return 0;
  163.     end
  164.  
  165.         -- Ignore if not my player.
  166.     if (self.Properties.bOnlyMyPlayer ~= 0 and entity ~= _localplayer) then
  167.         return 0;
  168.     end
  169.  
  170.     -- if only in vehicle - check if collider is in vehicle
  171.     if (self.Properties.bInVehicleOnly ~= 0 and not entity.theVehicle) then
  172.         return 0;
  173.     end
  174.  
  175.     if (entity.cnt.health <= 0) then 
  176.         return 0;
  177.     end
  178.  
  179.  
  180.     return 1;
  181. end
  182.  
  183.  
  184. -------------------------------------------------------------------------------
  185. -- Inactive State -------------------------------------------------------------
  186. -------------------------------------------------------------------------------
  187. ProximityTrigger.Inactive =
  188. {
  189.     OnBeginState = function( self )
  190.         printf("ProximityTrigger deactivating");
  191.         AI:RegisterWithAI(self.id, 0);
  192.     end,
  193.     OnEndState = function( self )
  194.         printf("ProximityTrigger activating");
  195.     end,
  196. }
  197. -------------------------------------------------------------------------------
  198. -- Empty State ----------------------------------------------------------------
  199. -------------------------------------------------------------------------------
  200. ProximityTrigger.Empty =
  201. {
  202.     -------------------------------------------------------------------------------
  203.     OnBeginState = function( self )
  204.         self.Who = nil;
  205.         self.UpdateCounter = 0;
  206.         self.Entered = 0;
  207.         if (self.Properties.aianchorAIAction~="") then
  208.             AI:RegisterWithAI(self.id, AIAnchor[self.Properties.aianchorAIAction]);
  209.         end
  210.     end,
  211.  
  212.     OnTimer = function( self )
  213.         self:GotoState( "Occupied" );
  214.     end,
  215.  
  216.     -------------------------------------------------------------------------------
  217.     OnEnterArea = function( self,entity,areaId )
  218.  
  219.  
  220.         if (self:IsValidSource(entity) == 0) then
  221.             return
  222.         end
  223.         
  224.  
  225.         
  226.         if (entity.ai==nil) then
  227.             if (self.Properties.bActivateWithUseButton~=0) then
  228.                 self.Who = entity;
  229.                 self:GotoState( "OccupiedUse" );
  230.                 do return end;
  231.             end
  232.         end
  233.         
  234.         if (self.Properties.EnterDelay > 0) then
  235.             if (self.Who == nil) then
  236.                 -- Not yet triggered.
  237.                 self.Who = entity;
  238.                 self:SetTimer( self.Properties.EnterDelay*1000 );
  239.             end
  240.         else
  241.             self.Who = entity;
  242.             self:GotoState( "Occupied" );
  243.         end
  244.     end,
  245.  
  246.  
  247. }
  248.  
  249. -------------------------------------------------------------------------------
  250. -- Occupied State ----------------------------------------------------------------
  251. -------------------------------------------------------------------------------
  252. ProximityTrigger.Occupied =
  253. {
  254.     -------------------------------------------------------------------------------
  255.     OnBeginState = function( self )
  256.         self:Event_Enter(self.Who);
  257. --        self:Do_Enter(self.Who);
  258.  
  259.         --self:Log("Enter");
  260.  
  261.         if(self.Properties.bKillOnTrigger==1)then
  262.             Server:RemoveEntity(self.id);
  263.         end
  264.     end,
  265.  
  266.     -------------------------------------------------------------------------------
  267.     OnTimer = function( self )
  268.         --self:Log("Sending on leave");
  269.         self:Event_Leave( self,self.Who );
  270.         if(self.Properties.bTriggerOnce~=1)then
  271.             self:GotoState("Empty");
  272.         end
  273.     end,
  274.  
  275.     -------------------------------------------------------------------------------
  276.     OnLeaveArea = function( self,entity,areaId )
  277.         -- Ignore if disabled.
  278.         --add a very small delay(so is immediate)
  279.         if (self:IsValidSource(entity) == 0) then
  280.             return
  281.         end
  282.         
  283.         if(self.Properties.ExitDelay==0) then
  284.             self.Properties.ExitDelay=0.01;
  285.         end
  286.         self:SetTimer(self.Properties.ExitDelay*1000);
  287.     end,
  288. }
  289.  
  290. -------------------------------------------------------------------------------
  291. -- OccupiedText State ---------------------------------------------------------
  292. -------------------------------------------------------------------------------
  293. ProximityTrigger.OccupiedUse =
  294. {
  295.     -------------------------------------------------------------------------------
  296.     OnBeginState = function( self )
  297.         self:EnableUpdate(1);
  298.     end,
  299.     -------------------------------------------------------------------------------
  300.     OnEndState = function( self )
  301.         self:EnableUpdate(0);
  302.     end,
  303.     -------------------------------------------------------------------------------
  304.     OnUpdate = function( self )
  305.  
  306.         if (self.Who.cnt) then            
  307.             if (not self.Who.cnt.use_pressed) then            
  308.                 Hud.label = self.Properties.TextInstruction;
  309.                 do return end;
  310.             end
  311.         end
  312.  
  313.         if (self.Properties.EnterDelay > 0) then
  314.             self:SetTimer( self.Properties.EnterDelay*1000 );
  315.         else
  316.             self:GotoState( "Occupied" );
  317.         end
  318.     end,
  319.     
  320.     -------------------------------------------------------------------------------
  321.     OnTimer = function( self )
  322.         self:GotoState( "Occupied" );
  323.     end,
  324.  
  325.     -------------------------------------------------------------------------------
  326.     OnLeaveArea = function( self,entity,areaId )
  327.         if (self.Who == entity) then
  328.             self:GotoState( "Empty" );
  329.         end
  330.     end,
  331. }